package com.choice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class enroll_preview_multi extends Activity {

	Button reviseBtn_prev_mul, enrollBtn_prev_mul;
	TextView textV_QuizTitle;
	ListView listV_prev_multi;
	String[] ans;
	String result, res;
	int arr_count;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.enroll_preview_multi);

		textV_QuizTitle = (TextView) findViewById(R.id.textV_QuizTitle);
		listV_prev_multi = (ListView) findViewById(R.id.listV_prev_multi);
		enrollBtn_prev_mul = (Button) findViewById(R.id.enrollBtn_prev_mul);

		textV_QuizTitle.setText(enroll_q_multi.title);
		textV_QuizTitle.setTextSize(20);
		enroll_q_multi.adapter = new ArrayAdapter<String>(
				enroll_preview_multi.this,
				android.R.layout.simple_expandable_list_item_1,
				enroll_q_multi.arraylist);
		listV_prev_multi.setAdapter(enroll_q_multi.adapter);
		enroll_q_multi.adapter.notifyDataSetChanged();
		listV_prev_multi.setAdapter(enroll_q_multi.adapter);

		enrollBtn_prev_mul.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// arr_count = enroll_q_multi.arraylist.size();
				arr_count = 4;
				ans = new String[arr_count];
				for (int i = 0; i < arr_count; i++) {
					ans[i] = enroll_q_multi.arraylist.get(i);
				}

				try {
					res = sendData(MainActivity.str_userID,enroll_q_multi.title, ans,
							"http://jsy6285.cafe24.com/multiple.html");
					
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();

					Toast.makeText(getApplicationContext(), "실패",
							Toast.LENGTH_LONG).show();

				}

				if (res != null) {

					new AlertDialog.Builder(enroll_preview_multi.this)
							// .setTitle("")
							.setIcon(R.drawable.icon)

							.setMessage("등록성공!")

							.setCancelable(true)

							.setPositiveButton("홈으로 가기",
									new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub
											Intent intent = new Intent(
													enroll_preview_multi.this,
													MainActivity.class);
											startActivity(intent);
											finish();

										}
									})

							.setNeutralButton("질문등록 계속",
									new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated method stub
											Intent intent = new Intent(
													enroll_preview_multi.this,
													enroll_q.class);
											startActivity(intent);
											finish();
										}
									}).show();
				}

				else {
					new AlertDialog.Builder(enroll_preview_multi.this)

							.setIcon(R.drawable.icon)

							.setMessage("등록실패")

							.setCancelable(true)

							.setPositiveButton("다시입력하세요",
									new DialogInterface.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											finish();
										}
									}).show();
				}

			}
		});

	}

	public String sendData(String no, String title, String[] ans, String url)
			throws ClientProtocolException, IOException, SocketException {

		try {

			HttpPost request = makeHttpPost(no, title, ans, url);
			HttpClient client = new DefaultHttpClient();
			ResponseHandler<String> reshandler = new BasicResponseHandler();
			result = client.execute(request, reshandler);

			if (result == null) {

				Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_LONG)
						.show();
			}
		} catch (Exception e) {
			e.printStackTrace();

			Toast.makeText(getApplicationContext(), "HTTPConn ect불가",
					Toast.LENGTH_LONG).show();
		}
		return result;
	}

	public HttpPost makeHttpPost(String $no, String $title, String[] $ans, String $url) {

		HttpPost request = new HttpPost($url);
		ArrayList<NameValuePair> nameValue = new ArrayList<NameValuePair>();
		nameValue.add(new BasicNameValuePair("no", $no));
		nameValue.add(new BasicNameValuePair("title", $title));
		nameValue.add(new BasicNameValuePair("ans", $ans[0]));
		nameValue.add(new BasicNameValuePair("ans2", $ans[1]));
		nameValue.add(new BasicNameValuePair("ans3", $ans[2]));
		nameValue.add(new BasicNameValuePair("ans4", $ans[3]));
		request.setEntity(makeEntity(nameValue));

		return request;
	}

	public HttpEntity makeEntity(ArrayList<NameValuePair> $nameValue) {
		HttpEntity result = null;
		try {
			result = new UrlEncodedFormEntity($nameValue, "euc-kr");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
}
